package menu.lt.pers.menu;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import menu.lt.pers.menu.Bean.HistoryBean;
import menu.lt.pers.menu.Bean.RecipeBean;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.dao.CollectionDao;
import menu.lt.pers.menu.dao.HistoryDao;

import java.util.ArrayList;
import java.util.List;

public class Fragment_my extends Fragment {
    private Button btn_collection;
    private Button btn_history;
    private TextView tv_title;
    private LinearLayout head_area;
    private LinearLayout linearlayout_box;
    private boolean login_state = false;
    private UserBean user;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frament_my,container,false);
        this.init(view);
        this.setListener();
        return view;
    }
    //更新 用户信息
    @Override
    public void onResume() {
        super.onResume();
        if (login_state){
            Log.i("Fragment_my.onResume","Fragment_my变为可见状态，即将重新获取用户信息...");
            this.updataUseInfo();
        }
    }

    private void updataUseInfo() {
        Log.i("Fragment_my.updataUseInfo","更新用户信息...");
        Bundle bundle = getArguments();
        if (bundle!=null){
            login_state = bundle.getBoolean("loginstate");
            user = bundle.getParcelable("user");
            Log.i("Fragment_my.updataUseInfo","更新用户信息完毕,当前登陆用户: "+user.getUsername());
            this.updateView();
        }
    }

    private void setListener() {
        head_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                //如果登录状态为true，则点击跳转修改信息界面
                if (!login_state){
                    intent = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(),UserInfoActivity.class);
                    intent.putExtra("user",user);
                    startActivity(intent);
                }
            }
        });

        btn_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (login_state){
                    intent = new Intent(getActivity(),HistoryActivity.class);
                    intent.putExtra("user",user);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        btn_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (login_state){
                    intent = new Intent(getActivity(),CollectionActivity.class);
                    intent.putExtra("user",user);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private void init(View view){
        btn_collection = view.findViewById(R.id.btn_collect);
        btn_history = view.findViewById(R.id.btn_history);
        head_area = view.findViewById(R.id.head_area);
        tv_title = view.findViewById(R.id.tv_title);
        linearlayout_box = view.findViewById(R.id.linearlayout_box);
        Bundle bundle = getArguments();
        if (bundle!=null){
            login_state = bundle.getBoolean("loginstate");
            user = bundle.getParcelable("user");
        }
        Log.i("Fragment_my.login_state","用户登录状态"+login_state);
        if (login_state){
            linearlayout_box.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(),UserInfoActivity.class);
                    intent.putExtra("user",user);
                    startActivity(intent);
                }
            });
            if (user.getUsername()!=null){
                tv_title.setText(user.getUsername());
            } else tv_title.setText("点击编辑个人信息");
        }
    }
    private void updateView(){
        if (user.getUsername()!=null){
            tv_title.setText(user.getUsername());
        } else tv_title.setText("点击编辑个人信息");
    }
}
